package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Sketch;
import com.opensymphony.xwork2.ActionSupport;


public class SketchAction extends ActionSupport {
	
	@SuppressWarnings({ "unused", "unchecked" })
	public void new_sketch() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String geoCode = request.getParameter("geoCode");
		String geoCodeType = request.getParameter("geoCodeType");
		String time = request.getParameter("time");
		String features = request.getParameter("features");
		
		JSONArray jsonObj = JSONArray.fromObject(features);
		List<String> featuresArray =(List<String>)jsonObj;
		Boolean rlt = true;
		for(int i=0;i<featuresArray.size();i++){
			JSONObject feature=jsonObj.getJSONObject(i);
			Sketch sketch = new Sketch(null,feature.toString(),geoCode,time,geoCodeType);
			try {
				rlt=DbTools.saveObj(sketch);
			} catch (Exception e) {
				ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
	}
	
	public void query_sketch() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String geoCode = request.getParameter("geoCode");
		String time = request.getParameter("time");
		String geoCodeType = request.getParameter("geoCodeType");
		Boolean rlt = true;
		String hql = "from Sketch s where s.geoHazardId='"+geoCode+"' AND s.timeId='"+time+"' and jbqk0102='"+geoCodeType+"'";
		List<Sketch> result=(List<Sketch>)DbTools.findByHql(hql);
		String resultString = JSONArray.fromObject(result).toString();
		ServletActionContext.getResponse().getOutputStream().write(resultString.getBytes("utf-8"));
	}
	
	public void save_or_del_sketch() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String geoCode = request.getParameter("geoCode");
		String geoCodeType = request.getParameter("geoCodeType");
		String time = request.getParameter("time");
		String addedFeatures = request.getParameter("add_features");
		String delFeatures = request.getParameter("del_features");
		Boolean rlt=true;
		if(addedFeatures != "[]"){
			JSONArray jsonObj = JSONArray.fromObject(addedFeatures);
			List<String> featuresArray =(List<String>)jsonObj;
			for(int i=0;i<featuresArray.size();i++){
				JSONObject feature=jsonObj.getJSONObject(i);
				Sketch sketch = new Sketch(null,feature.toString(),geoCode,time,geoCodeType);
				try {
					rlt=DbTools.saveObj(sketch);
				} catch (Exception e) {
					rlt =false;
					ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
				}
			}
		}
		
		if(delFeatures!="[]"){
			JSONArray jsonArrayDel = JSONArray.fromObject(delFeatures);
			List<String> featuresArrayDel = (List<String>)jsonArrayDel;
			for(int i=0;i<featuresArrayDel.size();i++){
				String featureId=jsonArrayDel.getString(i);
				Integer id = Integer.parseInt(featureId.toString());
				
				try {
					rlt=DbTools.delete("delete from Sketch s where s.id="+id);
				} catch (Exception e) {
					rlt =false;
					ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
				}
			}
		}
		
		ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
	}	
}
